<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Withdr;
use App\Models\Shop;

class WithdrController extends BaseController
{
    
    /**
     * 提现列表
     * @param Request $request [description]
     */
    public function withdrList(Request $request) 
    {
        $status    = $request->input('status');
        $shopname  = $request->input('shopname');

        $beginTime = $request->input('beginTime');//开始时间

        $endTime   = $request->input('endTime');//结束时间

        $withdr    = new Withdr();

        $status    != null && $withdr = $withdr->where('status', $status);

        $shopname  != null && $withdr = $withdr->where('shopname', 'like', '%'.$shopname.'%');

        $beginTime != null && $withdr = $withdr->where('created_at', '>=', $beginTime);

        $endTime   != null && $withdr = $withdr->where('created_at', '<=', $beginTime);

        $data = $withdr->orderBy('created_at', 'desc')->paginate($this->pageSize)->toArray();

        //获得对应的店铺数据
        $shopArr = [];
        foreach ($data['data'] as $val) {

            $shopArr[] = $val['shopid'];
        }
        //获得对应的店铺数据 并处理
        $shopObj = Shop::whereIn('id', $shopArr)->get();

        foreach ($shopObj as $key => $value) {

            $dataArr[$value->id]['phone']    = $value->phone;
            $dataArr[$value->id]['shopuser'] = $value->shopuser;
        }

        //组装数据
        foreach ($data['data'] as $key => &$val) {
            
            $val['phone']    = isset($dataArr[$val['shopid']]['phone']) ? $dataArr[$val['shopid']]['phone'] : 0;
            $val['shopuser'] = $dataArr[$val['shopid']]['shopuser'];
        }

        return self::json($data);
    }

    /**
     * 修改提现状态
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function withdrStatus(Request $request) {

        $remark = $request->input('remark', '');

        $status = $request->input('status');// 状态 1 申请/未审 2 已审 3 已打款 4 拒绝

        $id     = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        //查询数据
        $Withdr = Withdr::find($id);

        if(empty($Withdr)) return self::error('id有误');

        if ($status == Withdr::STATUS_FINISHED) {
            //根据提现款项去商户帐号中减去金额
            $result = Shop::redMoney($Withdr->shopid, $Withdr->money);

            if ($result == false) {

                $msg = $result::$msg ? $result::$msg : '商户账户扣款失败';

                return  self::error($msg);
            }
        }
        
        $Withdr->remark = $remark;

        $Withdr->status = $status;

        $result = $Withdr->save();

        return ($result == false) ? self::error('状态更改失败') : self::success('状态更改成功');
    }

    /**
     * 获得单个数据
     * @return [type] [description]
     */
    public function withdrOne(Request $request) {

        $id     = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        $withdr = Withdr::find($id)->toArray();

        if(empty($withdr)) return self::error('id有误');

        //获得当前商户的金币
        $shop = Shop::find($withdr['shopid']);

        $withdr['money_end'] = ($shop->money + $shop->help_money) - $withdr['money'];
        $withdr['phone']     = $shop->phone;
        $withdr['shopuser']  = $shop->shopuser;
        return self::json($withdr);
    }

    /**
     * 获得对应的状态值
     * @param  [type] $status_str [description]
     * @return [type]             [description]
     */
    private function getStatus($status_str) {

        switch ($status_str) {
            case 'weishen':
                $status = WithdrList::STATUS_NO_EXAMINE; //未审核
                break;
            case 'yishen':
                $status = WithdrList::STATUS_HAS_EXAMINE; //未审核
                break;
            case 'dakuan':
                $status = WithdrList::STATUS_FINISHED; //未审核
                break;
            case 'jujue':
                $status = WithdrList::STATUS_REFUSE; //拒绝
                break;             
            default:
                $status = 0;
                break;
        }

        return $status;
    }
}
